home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / disk / misc / Mtools207.lha / Mtools-2.0.7 / src / amiga_devices.c next >
Encoding:
C/C++ Source or Header  |  1994-05-28  |  3.8 KB  |  182 lines

  1. /* -------------------------------------------------- 
  2.  |  NAME
  3.  |    devices
  4.  |  PURPOSE
  5.  |    handle exec devices in a standard way.
  6.  |  NOTES
  7.  | 
  8.  |  COPYRIGHT
  9.  |    Copyright (C) 1993  Christian E. Hopps
  10.  |
  11.  |    This program is free software; you can redistribute it and/or modify
  12.  |    it under the terms of the GNU General Public License as published by
  13.  |    the Free Software Foundation; either version 2 of the License, or
  14.  |    (at your option) any later version.
  15.  |
  16.  |    This program is distributed in the hope that it will be useful,
  17.  |    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  |    GNU General Public License for more details.
  20.  |
  21.  |    You should have received a copy of the GNU General Public License
  22.  |    along with this program; if not, write to the Free Software
  23.  |    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  |    
  25.  |  HISTORY
  26.  |    chopps - Oct 9, 1993: Created.
  27.  |    lkv    - May 27, 1994: modified a little for usage in Mtools/Amiga
  28.  +--------------------------------------------------- */
  29.  
  30. /* #include "util.h" */
  31. #include "amiga_devices.h"
  32.  
  33. void *
  34. zmalloc (size_t b)
  35. {
  36.     void *mem = (void *) malloc (b);
  37.     if (mem) {
  38.     memset (mem, 0, b);
  39.     }
  40.     return (mem);
  41. }
  42.  
  43. void
  44. zfree (void *mem)
  45. {
  46.     if (mem) 
  47.     free (mem);
  48. }
  49.  
  50.  
  51. char *
  52. alloc_string (char *s)
  53. {
  54.     char *d = (char *) malloc (strlen (s) + 1);
  55.     if (d) {
  56.     strcpy (d, s);
  57.     }
  58.     return (d);
  59. }
  60.  
  61. /* returns structure with device open. */
  62. struct device_data *
  63. alloc_device (char *name, ulong unit, ulong flags, ulong iosize)
  64. {
  65.     struct device_data *dd = zmalloc (sizeof (*dd));
  66.     if (NULL == dd) {
  67.     return (NULL);
  68.     }
  69.     dd->port = CreateMsgPort ();
  70.     if (NULL == dd->port) {
  71.     free_device (dd);
  72.     return (NULL);
  73.     }
  74.     dd->io = CreateIORequest (dd->port, iosize);
  75.     if (NULL == dd->io) {
  76.     free_device (dd);
  77.     return (NULL);
  78.     }
  79.     dd->name = copy_string (name);
  80.     if (NULL == dd->name) {
  81.     free_device (dd);
  82.     return (NULL);
  83.     }
  84.     dd->unit = unit;
  85.     dd->flags = flags;
  86.  
  87.     if (open_device (dd)) {
  88.     free_device (dd);
  89.     return (NULL);
  90.     }
  91.       
  92.     return (dd);
  93. }
  94.  
  95. void
  96. free_device (struct device_data *dd)
  97. {
  98.     if (dd) {
  99.     close_device (dd);
  100.     DeleteIORequest (dd->io);
  101.     DeleteMsgPort (dd->port);
  102.     zfree (dd->name);    
  103.     }
  104. }
  105.  
  106. int
  107. open_device (struct device_data *dd)
  108. {
  109.     int error = -1;
  110.     if (dd && !dd->open) {
  111.     error = OpenDevice (dd->name, dd->unit, dd->io, dd->flags);
  112.     if (!error) {
  113.         dd->open = 1;
  114.     } else {
  115.         dd->open = 0;
  116.     }
  117.     }
  118.     return (error);
  119. }
  120.  
  121. void
  122. close_device (struct device_data *dd)
  123. {
  124.     if (dd) {
  125.     if (dd->open) {
  126.         if(!CheckIO (dd->io)) {
  127.         AbortIO (dd->io);
  128.         WaitIO (dd->io);
  129.         }
  130.         CloseDevice (dd->io);
  131.         dd->open = 0;
  132.     }
  133.     }
  134. }
  135.  
  136. /* returns actual number of bytes written or -1 for error. */
  137. ulong
  138. device_read (struct device_data *dd, ulong offset, ulong bytes, void *buffer)
  139. {
  140.     struct IOStdReq *io = (struct IOStdReq *)dd->io;
  141.     io->io_Length = bytes;
  142.     io->io_Offset = offset;
  143.     io->io_Data = buffer;
  144.  
  145.     if (!device_do_command (dd, CMD_READ)) {
  146.     return (io->io_Actual);
  147.     }
  148.     return (-1);
  149. }
  150.  
  151. /* returns actual number of bytes written or -1 for error. */
  152. ulong
  153. device_write (struct device_data *dd, ulong offset, ulong bytes, void *buffer)
  154. {
  155.     struct IOStdReq *io = (struct IOStdReq *)dd->io;
  156.     io->io_Length = bytes;
  157.     io->io_Offset = offset;
  158.     io->io_Data = buffer;
  159.     if (!device_do_command (dd, CMD_WRITE)) {
  160.     return (io->io_Actual);
  161.     }
  162.     return (-1);
  163. }
  164.  
  165. /* returns the error from DoIO () */
  166. int
  167. device_do_command (struct device_data *dd, UWORD command)
  168. {
  169.     int error = -1;
  170.     if (dd) {
  171.     if (dd->open) {
  172.         dd->io->io_Command = command;
  173.         error = (int) DoIO (dd->io);
  174.     } else {
  175.         dd->io->io_Error = -1;
  176.     }
  177.     }
  178.     return (error);
  179. }
  180.     
  181.     
  182.